package me.keiwu.user.controller;

import me.keiwu.pub.data.ConfigType;
import me.keiwu.pub.data.ResponseV2;
import me.keiwu.pub.data.config.AppConfigBean;
import me.keiwu.pub.data.service.UserInfoBean;
import me.keiwu.pub.util.CacheUtil;
import me.keiwu.pub.util.ContextUtil;
import me.keiwu.pub.util.wx.CustomUtil;
import me.keiwu.user.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by kei on 2018/4/18.
 */

@RestController
@RequestMapping("/custom")
public class CustomController {


    private static final Logger logger = LoggerFactory.getLogger(CustomController.class);


    @Autowired
    private CustomUtil customUtil;
    @Autowired
    private UserService userService;
    @Autowired
    private CacheUtil cacheUtil;


    /**
     * 客服自动回复
     *
     * @param request
     * @return
     */
    @RequestMapping("reply")
    public ResponseV2 reply(HttpServletRequest request) {
        Long userId = ContextUtil.getLoginUserId(request);
        UserInfoBean bean = userService.getUserInfoCache(userId);
        AppConfigBean appConfig = cacheUtil.getConfig(ConfigType.APP_CONFIG, AppConfigBean.class);
        if (appConfig == null || StringUtils.isBlank(appConfig.getCustomReplyJson())) {
            logger.error("no app config!!");
            return ResponseV2.ok();
        }
        customUtil.sendCustomLinkMsg(bean.getOpenId(), appConfig.getCustomReplyJson());
        return ResponseV2.ok();
    }

}
